#include "DirectionalLightComponent.h"
#include "SceneSystem.h"

IEngineComponentRegistry directionalLightComponentRegistery("DirectionalLightComponent", DirectionalLightComponent::Deserialize);

DirectionalLightComponent::DirectionalLightComponent()
{
	this->direction = D3DXVECTOR3(1,1,1);
	this->color = D3DXVECTOR3(1,1,1);
	this->intensity = 1;
}

DirectionalLightComponent::DirectionalLightComponent(D3DXVECTOR3 direction, D3DXVECTOR3 color, float intensity)
{
	this->direction = direction;
	this->color = color;
	this->intensity = intensity;
}

DirectionalLightComponent::~DirectionalLightComponent(void)
{
}

D3DXVECTOR3 DirectionalLightComponent::getDirection()
{
	return direction;
}

void DirectionalLightComponent::setDirection(D3DXVECTOR3 direction)
{
	this->direction = direction;
}

D3DXVECTOR3 DirectionalLightComponent::getColor()
{
	return color;
}

void DirectionalLightComponent::setColor(D3DXVECTOR3 color)
{
	this->color = color;
}

float DirectionalLightComponent::getIntensity()
{
	return intensity;
}

void DirectionalLightComponent::setIntensity(float intensity)
{
	this->intensity = intensity;
}

void DirectionalLightComponent::Serialize(EnginePointer<ISerializerWriteContext>& context, char* name)
{
	context->StartItem(name, "DirectionalLightComponent", "1.0");
	context->SerializeProperty("componentId", this->componentId);
	context->SerializeProperty("intensity", intensity);
	context->SerializeProperty("colorR", color.x);
	context->SerializeProperty("colorG", color.y);
	context->SerializeProperty("colorB", color.z);
	context->SerializeProperty("directionX", direction.x);
	context->SerializeProperty("directionY", direction.y);
	context->SerializeProperty("directionZ", direction.z);
	context->EndItem(name);
}

IEngineComponent* DirectionalLightComponent::Deserialize(EnginePointer<ISerializerReadContext>& context)
{
	DirectionalLightComponent* directionalLight = new DirectionalLightComponent();
	int index = 0;
	char* childName = context->ChildName(index);

	while (childName != NULL)
	{
		if (strcmp(childName, "componentId") == 0)
		{
			directionalLight->componentId = context->ChildValueAsULongLong(index);
		}
		else if (strcmp(childName, "intensity") == 0)
		{
			float intensity = context->ChildValueAsFloat(index);
			directionalLight->setIntensity(intensity);
		}
		else if(strcmp(childName, "colorR") == 0)
		{
			D3DXVECTOR3 color = directionalLight->getColor();
			color.x = context->ChildValueAsFloat(index);
			directionalLight->setColor(color);
		}
		else if(strcmp(childName, "colorG") == 0)
		{
			D3DXVECTOR3 color = directionalLight->getColor();
			color.y = context->ChildValueAsFloat(index);
			directionalLight->setColor(color);
		}
		else if(strcmp(childName, "colorB") == 0)
		{
			D3DXVECTOR3 color = directionalLight->getColor();
			color.z = context->ChildValueAsFloat(index);
			directionalLight->setColor(color);
		}
		else if(strcmp(childName, "directionX") == 0)
		{
			D3DXVECTOR3 direction = directionalLight->getDirection();
			direction.x = context->ChildValueAsFloat(index);
			directionalLight->setDirection(direction);
		}
		else if(strcmp(childName, "directionY") == 0)
		{
			D3DXVECTOR3 direction = directionalLight->getDirection();
			direction.y = context->ChildValueAsFloat(index);
			directionalLight->setDirection(direction);
		}
		else if(strcmp(childName, "directionZ") == 0)
		{
			D3DXVECTOR3 direction = directionalLight->getDirection();
			direction.z = context->ChildValueAsFloat(index);
			directionalLight->setDirection(direction);
		}
		childName = context->ChildName(++index);
	}
	
	return directionalLight;
}

GUID DirectionalLightComponent::getSystemIdentifier()
{
	return SCENE_SYSTEM_GUID;
}

GUID DirectionalLightComponent::getComponentIdentifier()
{
	return DIRECTIONAL_LIGHT_COMPONENT_GUID;
}
